package ru.spies.loginOrRegistration;

import javax.servlet.http.HttpServletRequest;

import ru.spies.dbwork.DBWork;
import ru.spies.entities.Agent;
import ru.spies.entities.Country;
import ru.spies.entities.Department;
import ru.spies.entities.Role;
import ru.spies.entities.Siteuser;
import ru.spies.forms.LoginOrRegistrationForm;
import ru.spies.profileClasses.UserInfo;

public class RegistrationWork extends AbstractLoginOrRegistrationWork {

	private static RegistrationWork statThis;
	
	private static final String RETURN_TO_REGISTRATION="<input type=\"button\" class=\"inFormBtn\" value=\"Попробовать зарегестрироваться снова\" onclick=\"goRegister()\">";
	private static final String CONFIRM_ERROR="<p>Введенные пароли не совпадают!!!<p>"+RETURN_TO_REGISTRATION;
	private static final String DEPARTMENT_ERROR="<p>В стране нет таких отделов!!!</p>"+RETURN_TO_REGISTRATION;
	private static final String AGENT_ERROR="<p>В отделе нет таких агентов!!!</p>"+RETURN_TO_REGISTRATION;
	
	public RegistrationWork() {
		
	}
	
	public static RegistrationWork getInstance(){
		if (statThis==null){
			statThis = new RegistrationWork();
		}
		return statThis;
	}

	@Override
	protected String createForm(HttpServletRequest request) {
		String userName=request.getParameter("loginField");
		if (userName==null) return LoginOrRegistrationForm.getInstance().createFailureForm(EMPTY_LOGIN);
		String withPasswordGenerator=request.getParameter("withPasswordGenerator");
		String password;
		if (withPasswordGenerator!=null){
			password=PasswordGenerator.getInstance().getRandomPassword();
		} else {
			password=request.getParameter("passwordField");
			if (password==null) return LoginOrRegistrationForm.getInstance().createFailureForm(EMPTY_PASSWORD);
			String confirmedPassword=request.getParameter("confirmField");
			if (confirmedPassword==null) return LoginOrRegistrationForm.getInstance().createFailureForm(CONFIRM_ERROR);
			if (!password.equals(confirmedPassword)) return LoginOrRegistrationForm.getInstance().createFailureForm(CONFIRM_ERROR);
		}
		Siteuser newUser = new Siteuser();
		newUser.setUsername(userName);
		newUser.setPassword(PasswordGenerator.getInstance().preparePasswordToSave(password));
		Role role = UserInfo.getInstance().getNormalRole();
		role.addSiteuser(newUser);
		Country country = new Country();
		country.setParamForFindQuery(request.getParameterValues("countryName")[0]);
		country=(Country) DBWork.getInstance().findObjectFromTable(country);
		Department dep=null;
		for (Department i:country.getDepartments()){
			if (i.getId().intValue()==Integer.valueOf(request.getParameter("departmentName")).intValue()){
				dep=i;
			}
		}
		if (dep==null) return LoginOrRegistrationForm.getInstance().createFailureForm(DEPARTMENT_ERROR);
		Agent agent = null;
		for (Agent i : dep.getAgents()) {
			if (i.getId().intValue()==Integer.valueOf(request.getParameter("agentName")).intValue()){
				agent=i;
			}
		}
		if (agent==null) return LoginOrRegistrationForm.getInstance().createFailureForm(AGENT_ERROR);
		newUser.setAgent(agent);
		agent.setSiteuser(newUser);
		DBWork.getInstance().updateTableObject(newUser);
		return LoginOrRegistrationForm.getInstance().createAfterRegistrationForm(userName, password);
	}

}
